import {convertToStringifiedJson} from "@/utils/object";
import {get} from "@/utils/http/httpUtils";
import {error} from "@/utils/msg";

export const OTMixinInitMethod = {
    inject: ['tableAll', 'http'],
    created() {
        this.init()
    },
    methods: {
        initTable({selectBox, paging, optionJson, parameterDefault}) {
            if (optionJson) {
                let optionObj = JSON.parse(optionJson);
                this.setTableStyle(optionObj.table);
                this.copyParams(optionObj.table, this.table)
                if (this.table.border == "false") {
                    this.table.border = false;
                }
                this.pagination.enabled = paging == "1";
                if (this.pagination.enabled) {
                    this.copyParams(optionObj.pagination, this.pagination)
                }
                for (let event in optionObj.events) {
                    this.events[event] = (params) => {
                        eval(optionObj.events[event].code)
                    }
                }
            }
            if (this.table['show-overflow'] == undefined && this.table['showOverflow'] == undefined) {
                this.table['show-overflow'] = true
            }
            if (parameterDefault) {
                if (parameterDefault.indexOf("{") == 0) {
                    try {
                        parameterDefault = convertToStringifiedJson(parameterDefault)
                    } catch (e) {
                        console.error("参数", parameterDefault)
                        error("默认参数配置异常，请F12查看异常日志！")
                        throw new Error(e)
                    }
                    this.parameterDefault = parameterDefault;
                } else {
                    for (let kv of parameterDefault.toString().split(",")) {
                        let kvs = kv.split("=");
                        if (kvs.length == 2) {
                            this.parameterDefault[kvs[0]] = kvs[1];
                        }
                    }
                }
            }
            this.selectBox = selectBox;
        }, initDefaultValue() {
            for (let o of this.table.formConfig.items) {
                if (o.key && this.parameterDefault[o.key] != undefined) {
                    this.table.formConfig.data[o.key] = this.parameterDefault[o.key]
                }
            }
        }, init(initDataSource) {
            if (!this.uuid || !this.theme || (!this.initDataSource && initDataSource == undefined)) {
                return
            }
            return new Promise((resolve) => {
                get(this.http, '/online/init', {uuid: this.uuid, theme: this.theme}).then(res => {
                    this.setDataSource(res, resolve)
                });
            })
        }, setDataSource(res, resolve) {
            let {configColumns, toolBut, operationBut, tableConfig, formMainList, tableDelete} = res;
            this.tableConfig = tableConfig;
            this.initTable(tableConfig);
            this.initColumn(configColumns, tableConfig);
            this.initToolBtn(toolBut)
            this.initOperationBtn(operationBut)
            this.initFormMainList(formMainList)
            this.initTree(tableConfig)
            this.initDefaultValue();
            this.deleteParameter = tableDelete;
            if (resolve) {
                resolve(res)
            }
            this.tableInitOk(tableConfig)
        }, tableInitOk(tableConfig) {
            this.$nextTick(() => {
                if (this.tableAll) {
                    this.tableAll[tableConfig.theme] = this;
                }
                if (this.events['tableInitOk']) {
                    this.events['tableInitOk']()
                }
                this.$emit("initOk", this.table)
                if (tableConfig.init != 0) {
                    this.query();
                }
            })
        }, copyParams(option, data) {
            for (let item in option) {
                let obj = option[item];
                let sub = data[item]
                if (obj instanceof Object && obj.isFunction && obj.flag == "flag##function#") {
                    data[item] = (params) => {
                        let fun = null
                        try {
                            eval("fun = (params) => {" + obj.code + "}")
                            return fun(params)
                        } catch (e) {
                            console.error("执行cope异常：" + item + "\n" + obj.code + "\n" + e)
                        }

                    }
                    continue
                }
                if (sub == null || sub == undefined) {
                    data[item] = obj
                    continue
                }
                if (obj instanceof Object && sub instanceof Object) {
                    this.copyParams(obj, sub)
                } else {
                    data[item] = obj
                }
            }
        }
    }
}
